<!DOCTYPE html>
<title>Credential Manager: PasswordCredential basics.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
test(function() {
    var credential = new PasswordCredential({
        id: 'id',
        password: 'pencil',
        name: 'name',
        iconURL: 'https://example.com/icon.png'
    });

    assert_equals(credential.id, 'id');
    assert_equals(credential.name, 'name');
    assert_equals(credential.iconURL, 'https://example.com/icon.png');
    assert_equals(credential.type, 'password');
    assert_equals(credential.password, 'pencil');
}, 'Interfaces and attributes of PasswordCredential');

test(function() {
    assert_throws_dom("SyntaxError", function () {
        var credential = new PasswordCredential({
            id: 'id',
            password: 'pencil',
            name: 'name',
            iconURL: '-'
        });
    });
}, 'Construct a PasswordCredential with an invalid icon URL.');

test(function() {
    var credential = new PasswordCredential({
        id: 'id',
        password: 'pencil',
        name: 'name',
    });

    assert_equals(credential.id, 'id');
    assert_equals(credential.name, 'name');
    assert_equals(credential.iconURL, '');
    assert_equals(credential.type, 'password');
    assert_equals(credential.password, 'pencil');

}, 'Construct a PasswordCredential with an empty icon URL.');

test(function() {
    var credential = new PasswordCredential({
        id: 'id',
        password: 'pencil',
    });

    assert_equals(credential.id, 'id');
    assert_equals(credential.name, '');
    assert_equals(credential.iconURL, '');
    assert_equals(credential.type, 'password');
    assert_equals(credential.password, 'pencil');
}, 'Construct a PasswordCredential with an empty name and icon URL.');

test(function() {
    assert_throws_js(TypeError,
                     () => { new PasswordCredential(); });
    assert_throws_js(TypeError,
                     () => { new PasswordCredential({}); });
    assert_throws_js(TypeError,
                     () => { new PasswordCredential({ 'id': undefined }); });
    assert_throws_js(TypeError,
                     () => { new PasswordCredential({ 'id': '' }); });
    assert_throws_js(TypeError,
                     () => { new PasswordCredential({ 'password': undefined }); });
    assert_throws_js(TypeError,
                     () => { new PasswordCredential({ 'password': '' }); });
    assert_throws_js(TypeError,
                     () => { new PasswordCredential({ 'id': undefined, 'password': undefined }); });
    assert_throws_js(TypeError,
                     () => { new PasswordCredential({ 'id': undefined, 'password': '' }); });
    assert_throws_js(TypeError,
                     () => { new PasswordCredential({ 'id': undefined, 'password': undefined }); });
}, 'PasswordCredential objects require IDs and Passwords');

function verify_form_credential(f, credential) {
    assert_equals(credential.id, 'musterman');
    assert_equals(credential.name, 'friendly name');
    assert_equals(credential.iconURL, 'https://example.com/photo');
    assert_equals(credential.type, 'password');
    assert_equals(credential.password, 'sekrit');

    var password = f.querySelector('[name=thePassword]');
    password.value = "";
    assert_throws_js(TypeError, _ => new PasswordCredential(f));
    f.removeChild(password);
    assert_throws_js(TypeError, _ => new PasswordCredential(f));

    // Reset the password, do the same test for the ID.
    f.appendChild(password);
    password.value = "sekrit";
    credential = new PasswordCredential(f);
    assert_true(credential instanceof PasswordCredential);

    var id = f.querySelector('[name=theId]');
    id.value = "";
    assert_throws_js(TypeError, _ => new PasswordCredential(f));
    f.removeChild(id);
    assert_throws_js(TypeError, _ => new PasswordCredential(f));
}

test(_ => {
    var f = document.createElement('form');
    f.enctype = 'mUlTiPaRt/fOrm-dAtA';
    f.innerHTML = "<input type='text' name='theId' value='musterman' autocomplete='username'>"
        + "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password'>"
        + "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo'>"
        + "<input type='text' name='theExtraField' value='extra'>"
        + "<input type='text' name='theName' value='friendly name' autocomplete='name'>";

    var credential = new PasswordCredential(f);
    verify_form_credential(f, credential);
}, 'PasswordCredential creation from `HTMLFormElement` with a multipart enctype');

test(_ => {
    var f = document.createElement('form');
    f.enctype = 'aPplIcaTion/X-wWw-form-urLencoded';
    f.innerHTML = "<input type='text' name='theId' value='musterman' autocomplete='username'>"
        + "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password'>"
        + "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo'>"
        + "<input type='text' name='theExtraField' value='extra'>"
        + "<input type='text' name='theName' value='friendly name' autocomplete='name'>";

    var credential = new PasswordCredential(f);
    verify_form_credential(f, credential);
}, 'PasswordCredential creation from `HTMLFormElement` with a urlencoded enctype');

test(_ => {
    var f = document.createElement('form');
    f.innerHTML = "<input type='text' name='theId' value='musterman' autocomplete='username'>"
        + "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password'>"
        + "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo'>"
        + "<input type='text' name='theExtraField' value='extra'>"
        + "<input type='text' name='theName' value='friendly name' autocomplete='name'>";

    var credential = new PasswordCredential(f);
    verify_form_credential(f, credential);
}, 'PasswordCredential creation from `HTMLFormElement` with no enctype');

test(_ => {
    var f = document.createElement('form');
    f.innerHTML = "<input type='text' name='theId' value='musterman' autocomplete='email username'>"
        + "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password value1'>"
        + "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo value2'>"
        + "<input type='text' name='theExtraField' value='extra'>"
        + "<input type='text' name='theName' value='friendly name' autocomplete='name value3'>";

    var credential = new PasswordCredential(f);
    verify_form_credential(f, credential);
}, 'PasswordCredential creation from `HTMLFormElement` with multiple autocomplete attributes');

test(_ => {
    var f = document.createElement('form');
    f.innerHTML = "<input type='text' name='theId' value='musterman' autocomplete='email username'>"
        + "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password'>"
        + "<input type='text' value='https://example.com/photo' autocomplete='photo'>"
        + "<input type='text' value='extra'>"
        + "<input type='text' value='friendly name' autocomplete='name'>";

    var credential = new PasswordCredential(f);
    assert_equals(credential.id, 'musterman');
    assert_equals(credential.name, '');
    assert_equals(credential.iconURL, '');
    assert_equals(credential.type, 'password');
    assert_equals(credential.password, 'sekrit');
}, 'PasswordCredential creation from `HTMLFormElement` with missing name attributes');
</script>
